package com.ruoyi.control.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.StrFormatter;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.control.domain.Mainventilator1History;
import com.ruoyi.control.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.CentralpumphouseHistoryMapper;
import com.ruoyi.control.domain.CentralpumphouseHistory;
import com.ruoyi.control.service.ICentralpumphouseHistoryService;

/**
 * 中央水泵房Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class CentralpumphouseHistoryServiceImpl extends ServiceImpl<CentralpumphouseHistoryMapper, CentralpumphouseHistory> implements ICentralpumphouseHistoryService //修改
{
    @Autowired
    private CentralpumphouseHistoryMapper centralpumphouseHistoryMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询中央水泵房
     *
     * @param id 中央水泵房主键
     * @return 中央水泵房
     */
    @Override
    public CentralpumphouseHistory selectCentralpumphouseHistoryById(String id) {
        return centralpumphouseHistoryMapper.selectCentralpumphouseHistoryById(id);
    }

    /**
     * 查询中央水泵房列表
     *
     * @param centralpumphouseHistory 中央水泵房
     * @return 中央水泵房
     */
    @Override
    public List<CentralpumphouseHistory> selectCentralpumphouseHistoryList(CentralpumphouseHistory centralpumphouseHistory) {
        return centralpumphouseHistoryMapper.selectCentralpumphouseHistoryList(centralpumphouseHistory);
    }

    /**
     * 新增中央水泵房
     *
     * @param centralpumphouseHistory 中央水泵房
     * @return 结果
     */
    @Override
    public int insertCentralpumphouseHistory(CentralpumphouseHistory centralpumphouseHistory) {
        return centralpumphouseHistoryMapper.insertCentralpumphouseHistory(centralpumphouseHistory);
    }

    /**
     * 修改中央水泵房
     *
     * @param centralpumphouseHistory 中央水泵房
     * @return 结果
     */
    @Override
    public int updateCentralpumphouseHistory(CentralpumphouseHistory centralpumphouseHistory) {
        centralpumphouseHistory.setUpdateTime(DateUtils.getNowDate());
        return centralpumphouseHistoryMapper.updateCentralpumphouseHistory(centralpumphouseHistory);
    }

    /**
     * 批量删除中央水泵房
     *
     * @param ids 需要删除的中央水泵房主键
     * @return 结果
     */
    @Override
    public int deleteCentralpumphouseHistoryByIds(String[] ids) {
        return centralpumphouseHistoryMapper.deleteCentralpumphouseHistoryByIds(ids);
    }

    /**
     * 删除中央水泵房信息
     *
     * @param id 中央水泵房主键
     * @return 结果
     */
    @Override
    public int deleteCentralpumphouseHistoryById(String id) {
        return centralpumphouseHistoryMapper.deleteCentralpumphouseHistoryById(id);
    }

    @Override
    public Integer[] selectByTagName(String tagName) {
        Integer[] history = new Integer[60 * 24];
        List<CentralpumphouseHistory> historyList = centralpumphouseHistoryMapper.selectByTagName(tagName);
        for (int i = 0; i < 60 * 24; i++) {
            if (i >= historyList.size()) {
                history[i] = 0;
            } else {
                double doubleValue = Double.parseDouble(historyList.get(i).getValue());
                int intValue = (int) doubleValue; // 如果需要四舍五入，可以使用 Math.round(doubleValue)
                history[i] = intValue;
            }
        }
        return history;
    }

    @Override
    public int getWaterTotal() {
        final String key = "psKey";
        int total = 0;
        String cachedValue = redisCache.getCacheObject(key);

        List<ElectricityHistoryDto> list = centralpumphouseHistoryMapper.getWaterTotal();
        total = list.size();
        redisCache.setCacheObject(key, String.valueOf(total));

//        if (cachedValue == null || cachedValue.isEmpty()) {
//            List<ElectricityHistoryDto> list = centralpumphouseHistoryMapper.getWaterTotal();
//            total = list.size();
//            redisCache.setCacheObject(key, String.valueOf(total));
//        } else {
//            total = Integer.parseInt(cachedValue);
//            System.out.println("读取psKey缓存: " + total);
//        }
        return Integer.parseInt(cachedValue);
    }


    @Override
    public List<String> getWeekWaterTotal() {
        List<WaterDto> dtoList = centralpumphouseHistoryMapper.getWeekWaterTotal();


        ArrayList<String> valueList = new ArrayList<>();
        if (dtoList.size() > 0){
            //遍历
            dtoList.stream().map(objInList -> {
                String total = objInList.getTotal();
                valueList.add(total);
                return valueList;
            }).collect(Collectors.toList());
        }else {
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
        }

        return valueList;
    }


    /**
     * 水泵房7天故障
     * @return
     */
    public List<CentralpumphouseHistoryDto> getWaterTotalFault() {


        final String key = "sbfGzKey";
        List<CentralpumphouseHistoryDto> cachedValueList = redisCache.getCacheObject(key);

        List<CentralpumphouseHistoryDto> list = centralpumphouseHistoryMapper.getWaterTotalFault();


        if (list == null || list.isEmpty()) {
            //list  为空
            List<CentralpumphouseHistoryDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                CentralpumphouseHistoryDto abnormalCountDto = new CentralpumphouseHistoryDto();
                abnormalCountDto.setCount("0");
                defaultList.add(abnormalCountDto);
            }
            List<String> collect = defaultList.stream().map(objInList -> {
                String abnormalCount = objInList.getCount();
                return abnormalCount;
            }).collect(Collectors.toList());
            redisCache.setCacheObject(key, collect);
        } else {
            List<CentralpumphouseHistoryDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                CentralpumphouseHistoryDto abnormalCountDto = new CentralpumphouseHistoryDto();
                abnormalCountDto.setCount("0");
                if (abnormalCountDto.getUpdateDate() != null) {
                    abnormalCountDto.setUpdateDate(abnormalCountDto.getUpdateDate());
                }
                resultList.add(abnormalCountDto);
            }

            redisCache.setCacheObject(key, resultList);

        }

        return cachedValueList;

    }

    public List<CentralpumphouseHistoryDto> getWaterPowerValueList(String str) {
        String[] keys = {"sbfGhKey1", "sbfGhKey2", "sbfGhKey3"};
        String[] prefixes = {"1#", "2#", "3#"};

        int index = Integer.parseInt(str) - 1;
        String key = keys[index];
        String dy = prefixes[index] + "电机AB线电压";
        String dl = prefixes[index] + "电机A相电流";

        List<CentralpumphouseHistoryDto> cachedValueList = redisCache.getCacheObject(key);
        if (cachedValueList != null) {
            return cachedValueList;
        }

        List<CentralpumphouseHistoryDto> list = centralpumphouseHistoryMapper.getWaterPowerValueList(dy, dl);
        List<CentralpumphouseHistoryDto> processedList = list.stream().map(objInList -> {
            Double avgPower = objInList.getAvgPower();
            Double consumptionValue = avgPower * 60 * 60;
            objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", consumptionValue)));
            return objInList;
        }).collect(Collectors.toList());

        redisCache.setCacheObject(key, processedList);
        return processedList;
    }
}
